<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="isClosingDialog=true">{{ $t('market.filter') }}</el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.period') }}</span>
        <el-input v-model="period" size="mini" disabled />
      </div>
      <!--      当前关账期间-->
      <dataRow />
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        get-row-keys="UUID"
        serial
        sortable
        :tree-props="{children: 'detailList', hasChildren: 'hasChild'}"
      >
        <!--   来源单ju    -->
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="pushFrombill(col.scope.row)">{{ col.scope.row.fromBillNo }}</el-link>
        </template>
        <!--   工单编号    -->
        <template v-slot:btnState="col">
          <el-link type="primary" @click="pushmob(col.scope.row)">{{ col.scope.row.moBillNo }}</el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
      </jc-table>
      <jc-pagination
        :total="subDetails.total"
        :page.sync="subDetails.pageNum"
        :limit.sync="subDetails.pageSize"
        @pagination="handleGetUntreated"
      />
    </div>
    <!-- 选择时间 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.checkPeriod')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="600px"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.startPeriod')">
            <el-date-picker
              v-model="subDetails.start"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择期间"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endPeriod')">
            <el-date-picker
              v-model="subDetails.end"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择期间"
            />
          </el-form-item>
          <el-form-item :label="$t('market.srcbillno')">
            <el-input v-model.trim="subDetails.moBillNo" />
          </el-form-item>
          <el-form-item :label="$t('market.paezText1')">
            <el-input v-model.trim="subDetails.salKhddh" />
          </el-form-item>
          <el-form-item :label="$t('market.expenseite')">
            <el-input v-model.trim="subDetails.fyxmName" />
          </el-form-item>
          <el-form-item :label="$t('market.deaprt')">
            <el-input v-model.trim="subDetails.deptName" />
          </el-form-item>
          <el-form-item :label="$t('market.fmaterial')">
            <el-input v-model.trim="subDetails.fnumber" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryCalComponentList, queryRedirectUrl
} from '@/api/financial/warehousing'
import dataRow from '@/views/financial/company/components/dataRow'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  name: 'InventoryDetail',
  components: {
    dataRow
  },
  mixins: [searData],
  data() {
    return {
      subDetails: {
        start: '',
        end: '',
        deptName: '',
        fnumber: '',
        salKhddh: '',
        fyxmName: '',
        moBillNo: '',
        pageSize: 200,
        pageNum: 1,
        total: 0
      },
      openDelay: 1000,
      period: '',
      loading: false,
      isClosingDialog: false,
      // 表头
      tableData: [], // 存货收发存明细表
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.deaprt'), prop: 'deptName' },
        { label: this.$t('market.businessType'), prop: 'billtypeName' },
        { label: this.$t('market.frombillNo'), type: 'tag', prop: 'fromBillNo', width: '120' },
        { label: this.$t('market.srcbillno'), type: 'state', prop: 'moBillNo' },
        { label: this.$t('market.workSeq'), prop: 'moSeq', width: '80' },
        { label: this.$t('market.paezText1'), prop: 'salKhddh' },
        { label: this.$t('market.productNo'), type: 'order', prop: 'fnumber' },
        { label: this.$t('market.productName'), prop: 'matName' },
        { label: this.$t('market.productmolde'), prop: 'fmodel', width: '150' },
        { label: this.$t('market.billNo'), prop: 'collectBillNo' },
        { label: this.$t('market.expenseite'), prop: 'fyxmName' },
        { label: this.$t('market.billamount'), prop: 'famount', align: 'right', width: '80' }
      ]
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      const { start, end } = this.subDetails
      this.period = start + ' -- ' + end
      this.handleGetUntreated()
    },
    // 获取成本结算单列表数据
    async handleGetUntreated() {
      this.loading = true
      const DATA = { ...this.subDetails }
      const RES = await queryCalComponentList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
        this.subDetails.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass() {
      this.currentPage = 1
      this.handleGetUntreated()
    },
    // 跳转生产委外订单
    pushmob(ev) {
      const { prdOrSub, moBillNo } = ev
      if (prdOrSub === 1) { // 生产
        this.$router.push({ path: `/proOrderDetails/${moBillNo}` })
      } else if (prdOrSub === 2) { // 委外
        this.$router.push({ path: `/outOrderDetails/${moBillNo}` })
      }
    },
    // 来源单据跳转页面
    pushFrombill(ev) {
      const DATA = {
        fbillNo: ev.fromBillNo,
        enumId: ev.fbilltypeId
      }
      queryRedirectUrl(DATA).then(res => {
        if (res.code === 0) {
          this.$router.push({ path: res.data.redirectUrl })
        }
      })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryCalComponentList({
            ...this.subDetails,
            pageSize: 100000,
            pageNum: 1
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '费用分配明细表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
